//载入那些函数库
let includes = ([
    "include/common.js",
]);
//继承哪些JS 在所有代码之前要完成继承
let inherits = ([
    "inherit/data.js",
]);


function create() {
    let me = thisObject;
    //需要心跳
    me.set("heartbeat", true);
    return me;
}



function heartbeat() {


}


function calcResult(ob1, ob2) {
    let win, lost, ok = false;
    if (ob1.getAttackUnit().length <= 0) {
        win = ob2;
        lost = ob1;
        ok = true;
    }

    if (ob2.getAttackUnit().length <= 0) {
        win = ob1;
        lost = ob2;
        ok = true;
    }

    if (ok) {
        tellObject(win.get("ownUser"), "战斗胜利！").to();
        tellObject(lost.get("ownUser"), "战斗失败！").to();
        //解除战斗关系
        win.get("ownUser").set("simKiller", 0);
        lost.get("ownUser").set("simKiller", 0);
        return true;
    }

    return false;

}


// function calcResult(win, lost) {
//
//     if (victim.length <= 0) {
//         calcResult(killerOb, victimOb);
//         return;
//     }
//
//     if (killer.length <= 0) {
//         calcResult(victimOb, killerOb);
//         return;
//     }
//
//
//
//     if( win.get() ){
//
//     }
//
//
//
//
//     tellObject(win.get("ownUser"), "战斗胜利！").to();
//     tellObject(lost.get("ownUser"), "战斗失败！").to();
//     //解除战斗关系
//     win.get("ownUser").set("simKiller", 0);
//     lost.get("ownUser").set("simKiller", 0);
// }


function combatMessageToKiller(str, killer, victim) {
    let msg = str;
    msg = msg.replace(/\$N/g, "你").replace(/\$n/g, victim.get("name"));
    tellObject(killer, msg).to();
}

function combatMessageToVictim(str, killer, victim) {
    let msg = str;
    msg = msg.replace(/\$N/g, killer.get("name")).replace(/\$n/g, "你");
    tellObject(victim, msg).to();

}


function combatMessage(str, ob, you) {
    let k = ob.get("ownUser"), v = you.get("ownUser");
    combatMessageToKiller(str, k, v);
    combatMessageToVictim(str, k, v);
}


function doKill(killerOb, victimOb) {

    let meSimOb,vSimOb;

    if (!objectp(meSimOb = killerOb.get("simOb"))){
        return ;
    }

    if (!objectp(vSimOb = victimOb.get("simOb"))){
        return ;
    }

    os.worker.create(() => {
        while (true){
            if( calcResult(meSimOb,vSimOb) ) break;
            kill(meSimOb, vSimOb);
            if( calcResult(meSimOb,vSimOb) ) break;
            kill(vSimOb, meSimOb);
        }
    }).start();



}


function kill(killerOb, victimOb) {


    let me = thisObject;

    let victim = victimOb.getAttackUnit();
    let killer = killerOb.getAttackUnit();

    if (victim.length <= 0 || killer.length <= 0) return;


    victim = getRandomOneOfArr(victim);
    killer = getRandomOneOfArr(killer);
    let cmbStr, damage = killer.attack, def = victim.def;
    damage = damage - def;
    // sendOwn(`${killer.name} 攻击了 ${victim.name}`);

    cmbStr = "$N的 " + killer.name + " 攻击了 $n的 " + victim.name;


    if (damage > 0) {
        victim.hp -= damage;
        if (victim.hp <= 0) {
            cmbStr += ",$n的" + victim.name + "被歼灭了。";
        } else {
            cmbStr += ",$n的" + victim.name + "受到了< " + damage + " > 点伤害。";
        }
        combatMessage(cmbStr, killerOb, victimOb);
    } else {

        cmbStr += ",但 $n 的" + victim.name + "并没有受伤。";
        combatMessage(cmbStr, killerOb, victimOb);
    }

}




